/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.javasyp.checker;

import java.util.Arrays;

/**
 * @author kevin
 * @since Mar 27, 2010
 */
public class ArrayType implements RefType {

	private ObjectType refType;
	private PermKind[] dimTypes;

	ArrayType(ObjectType refType, PermKind... dimTypes) {
		if (dimTypes.length == 0)
			throw new IllegalArgumentException("No array dims");
		this.refType = refType;
		this.dimTypes = dimTypes;
	}

	public PermKind getPerm() {
		return refType.getPerm();
	}

	public boolean isBorrowed() {
		return refType.isBorrowed();
	}

	public boolean isConsumed() {
		return refType.isConsumed();
	}

	public boolean isReleased() {
		return refType.isReleased();
	}

	public boolean isStrongerThan(RefType other) {
		if (other == null)
			return true;
		if (! (other instanceof ArrayType))
			// incomparable
			return false;
		ArrayType o = (ArrayType) other;
		if (dimTypes.length != o.dimTypes.length)
			// incomparable
			return false;
		if (! refType.isStrongerThan(o.refType))
			return false;
		for (int i = 0; i < dimTypes.length; ++i) {
			if (! dimTypes[i].isStrongerThan(o.dimTypes[i]))
				return false;
		}
		return true;
	}

	public RefType join(RefType other) {
		if (other == null)
			return null;
		if (! (other instanceof ArrayType))
			// incomparable
			throw new IllegalArgumentException("Not an array: " + other);
		ArrayType o = (ArrayType) other;
		if (dimTypes.length != o.dimTypes.length)
			// incomparable
			throw new IllegalArgumentException("Different array depth: " + other);
		RefType joinedRef = refType.join(o.refType);
		PermKind[] joinedDims = new PermKind[dimTypes.length];
		for (int i = 0; i < joinedDims.length; ++i)
			joinedDims[i] = dimTypes[i].join(o.dimTypes[i]);
		return ObjectType.getInstance(joinedRef.getPerm(), joinedRef.isBorrowed(), joinedRef.isReleased(), joinedDims);
	}

	public RefType leak(PermKind p) {
		RefType ref = refType.leak(p);
		// don't weaken dimTypes; Scope.accessArrayElement will take care of that as needed
		if (ref == refType)
			return this;
		else
			return new ArrayType((ObjectType) ref, dimTypes);
	}

	public PermKind[] getElemDims() {
		return dimTypes;
	}

//	public boolean isBorrowed(int dim) {
//		return dimTypes[dim].isBorrowed();
//	}
//
//	public boolean isConsumed(int dim) {
//		return dimTypes[dim].isConsumed();
//	}
//
//	public boolean isReleased(int dim) {
//		return dimTypes[dim].isReleased();
//	}
//
//	public ObjectType leak(PermKind p, int dim) {
//		return dimTypes[dim].leak(p);
//	}

	@Override
	public String toString() {
		return refType + Arrays.toString(dimTypes);
	}
}
